package com.xiaxiang.xiawan.Home.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.donggua.aichebao.commonui.refresh.XXPullToRefreshRecyclerViewAdapt;
import com.xiaxiang.xiawan.Home.HomeData;
import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.adapt.OnItemClickListener;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by Administrator on 2017-11-06.
 */

public class VideoRefreshAdapt extends XXPullToRefreshRecyclerViewAdapt {
    public List<HomeData> mHomeDataList;
    private Context mContext;
    private OnItemClickListener mListener;
    private OnVideoItemClick mVideoItemClick;
    public VideoRefreshAdapt(Context context, List<HomeData> data,OnVideoItemClick videoItemClick) {
      //  super(context, data);
        mContext = context;
        mHomeDataList = data;
        mVideoItemClick = videoItemClick;
    }
    public VideoRefreshAdapt(Context context, List<HomeData> data) {
        //  super(context, data);
        mContext = context;
        mHomeDataList = data;
    }
    public void addHomeDataByChange(HomeData data){
        mHomeDataList.add(data);
        notifyDataSetChanged();
    }

    public void deleteHomeData(int index){
        mHomeDataList.remove(index);
        notifyDataSetChanged();
    }

    public void addHomeData(HomeData data){
        mHomeDataList.add(data);
        //  notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return mHomeDataList.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.adp_item_showreel, parent,
                false));
        holder.itemView.setTag(holder.getAdapterPosition());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener != null){
                    int position = holder.getAdapterPosition();
                    mListener.onItemClick(view,position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int position = holder.getAdapterPosition();
                mListener.onItemLongClick(view,position);
                return true;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HomeData item = mHomeDataList.get(position);
        MyViewHolder myViewHolder = (MyViewHolder)holder;
        myViewHolder.tvTitle.setText(item.mUserName);
        myViewHolder.tvProductionName.setText(item.mName);
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<ImageView>(myViewHolder.mIcon );
        ImageView weekImage = imageViewWeakReference.get();
       // Glide.with(mContext).load(Uri.parse("file://" + item.mThumbnailUrl)).into(weekImage);
        myViewHolder.imgLike.setTag(position);
        myViewHolder.imgShare.setTag(position);
        myViewHolder.imgLike.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                int pos = (int)view.getTag();
                if(mVideoItemClick != null){
                    mVideoItemClick.onClickLiken(pos);
                }
            }
        });

        myViewHolder.imgShare.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                int pos = (int)view.getTag();
                if(mVideoItemClick != null){
                    mVideoItemClick.onClickShare(pos);
                }
            }
        });
        Glide.with(mContext).load(item.mThumbnailUrl).into(weekImage);
        String iconurl = item.mHeadPhoto;
        if (iconurl != null && iconurl.length() > 0) {
            Glide.with(mContext).load(iconurl)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(myViewHolder.imgHead).onLoadFailed(mContext.getResources().getDrawable(R.mipmap.home_head));
        }
    }

    public void clearData(){
        mHomeDataList.clear();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvProductionName;
        ImageView mIcon;
        ImageView imgShare;
        ImageView imgLike;
        ImageView imgHead;
        public MyViewHolder(View view) {
            super(view);
            //tv = (TextView) view.findViewById(android.R.id.text1);
             mIcon = (ImageView)view.findViewById(R.id.imgVideoImg);
             tvTitle =  (TextView)view.findViewById(R.id.tvVideoName);
            tvProductionName = (TextView)view.findViewById(R.id.tvProductionName);
            imgShare = (ImageView)view.findViewById(R.id.imgShare);
            imgLike = (ImageView)view.findViewById(R.id.imgLike);
            imgHead = view.findViewById(R.id.imgHead);

        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public interface OnVideoItemClick{
        public void onClickShare(int index);
        public void onClickLiken(int index);
    }
}
